<?php
/**
 * Created by PhpStorm.
 * User: xiaoqi
 * Date: 18-9-18
 * Time: 下午2:14
 */

namespace App\HttpController;

use EasySwoole\Core\Http\AbstractInterface\Controller;
use Illuminate\Database\Capsule\Manager as Capsule;

class Db extends Controller
{
    /**
     * 测试数据库连接
     */
    public function index()
    {
        $version = Capsule::select('select version();');
        $this->response()->write(json_encode($version));
    }

    /**
     * 创建用户表
     */
    public function users()
    {
        // 删除
        Capsule::schema()->dropIfExists('users');
        // 创建
        Capsule::schema()->create('users', function ($table) {
            $table->increments('id');
            // 唯一索引
            $table->string('name')->unique();
            $table->string('password')->default('');
            $table->string('email')->default('');
            $table->string('mobile')->default('');
            $table->string('nick_name')->default('');
            $table->string('avatar_url')->default('');
            $table->string('open_id')->default('');
            $table->string('real_name')->default('');
            $table->integer('sex')->default(0);
            $table->integer('created_uid')->default(0);
            $table->integer('status')->default(1);
            $table->timestamps();
            // 复合索引
            $table->index(['email', 'mobile', 'open_id']);
        });
    }

    /**
     * 创建菜单表
     */
    public function menus()
    {
        // 删除
        Capsule::schema()->dropIfExists('menus');
        // 创建
        Capsule::schema()->create('menus', function ($table) {
            $table->increments('id');
            // 唯一索引
            $table->string('name')->unique();
            $table->string('icon_str')->default('');
            $table->string('title')->default('');
            $table->string('permissions')->default('');
            $table->string('description')->default('');
            $table->integer('created_uid')->default(0);
            $table->integer('status')->default(1);
            $table->timestamps();
        });
    }

    /**
     * 创建rbac权限的表
     */
    public function rbac()
    {
        /**
         * 权限表
         */
        Capsule::schema()->dropIfExists('permissions');
        Capsule::schema()->create('permissions', function ($table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->default('');
            $table->string('description')->default('');
            $table->integer('created_uid')->default(0);
            $table->timestamps();
        });

        /**
         * 角色表
         */
        Capsule::schema()->dropIfExists('roles');
        Capsule::schema()->create('roles', function ($table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->default('');
            $table->string('description')->default('');
            $table->integer('created_uid')->default(0);
            $table->timestamps();
        });

        /**
         * 角色权限关联表
         */
        Capsule::schema()->dropIfExists('permission_role');
        Capsule::schema()->create('permission_role', function ($table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();

            $table->foreign('permission_id')
                ->references('id')->on('permissions')
                ->onDelete('cascade');

            $table->foreign('role_id')
                ->references('id')->on('roles')
                ->onDelete('cascade');

            $table->primary(['permission_id', 'role_id']);
        });

        /**
         * 角色用户关联表
         */
        Capsule::schema()->dropIfExists('role_user');
        Capsule::schema()->create('role_user', function ($table) {
            $table->integer('role_id')->unsigned();
            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');

            $table->foreign('role_id')
                ->references('id')->on('roles')
                ->onDelete('cascade');

            $table->primary(['user_id', 'role_id']);
        });
    }
}